<template>
  <div class="wrapper">
    <div
        style="margin: 250px auto; background-color: #fff; width: 420px; height: 300px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 26px"><b>食品安全信用数据归集系统</b></div>
      <el-form :model="people" :rules="rules" ref="userForm">
        <el-form-item prop="peopleids">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="people.peopleids"></el-input>
        </el-form-item>
        <el-form-item prop="peoplepassword">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="people.peoplepassword"></el-input>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: right">
          <el-button type="primary" size="medium" autocomplete="off" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {setRoutes} from "@/router";

export default {
  name: "Login",
  data() {
    return {
      people: {},
      rules: {
        peopleids: [
          {required: true, message: '请输入工号', trigger: 'blur'},
          {min: 10, max: 10, message: '长度为 10 个字符', trigger: 'blur'}
        ],
        peoplepassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
        ],
      },
    }
  },
  created() {
    localStorage.setItem("people", JSON.stringify(res.data))
  },
  methods: {
    login: function () {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {  //表单校验合法
          this.request.post("http://localhost:9090/people/login", this.people).then(res => {
            if (res.code == 200) {
              localStorage.setItem("people", JSON.stringify(res.data))  //存储用户信息到浏览器
              localStorage.setItem("menus", JSON.stringify(res.data.menus))  //存储用户信息到浏览器

              //动态设置当前用户路由
              setRoutes()
              this.$router.push("/echarts1")
              this.$message.success("登录成功")
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      });
    }
  }
}
</script>

<style>
.wrapper {
  background-image: url("/src/assets/background.jpg");
  width:100%;
  height:100%;
  position:fixed;
  background-size:100% 100%;
}
</style>
